import del from './atom/db/del';
import query from './atom/db/query';

declare const action_id: string;
declare const session_id: string;
declare const spaceid: string;

interface Message {
	deptid: string
}

async function atom(msg: Message) {
	const systemid = '6051';
	const { exec, prepare } = del(action_id, session_id, spaceid, systemid);

	const { exec: e1, prepare: p1 } = query(action_id, session_id, spaceid, systemid);
	p1('dept', ['_id'], { deptid: msg.deptid });//query 部门_id
	p1('teach_staff', ['_id', 'staff_number'], { deptid: msg.deptid });//query 部门下的教职工
	const [depts, staffs] = await e1();
	console.log('--->', depts, staffs);
	prepare('dept', [{ _id: depts[0]._id }]);// del 部门
	if (staffs.length > 0) {
		const { exec: e2, prepare: p2 } = query(action_id, session_id, spaceid, systemid);
		staffs.forEach((s: { _id: string, staff_number: string }) => {
			prepare('teach_staff', [{ _id: s._id }]);// del 部门下的教职工
			p2('position_contact', ['_id'], { staff_number: s.staff_number });//query 职位及联系方式
			p2('work_experience', ['_id'], { staff_number: s.staff_number });//query 工作经历
			p2('edu_experience', ['_id'], { staff_number: s.staff_number });//query 教育经历
		});
		const [position_contacts, work_experiences, edu_experiences] = await e2();
		position_contacts.forEach((d: { _id: string }) => {
			prepare('position_contact', [{ _id: d._id }]);//del 职位及联系方式
		});
		work_experiences.forEach((d: { _id: string }) => {
			prepare('work_experience', [{ _id: d._id }]);//del 工作经历
		});
		edu_experiences.forEach((d: { _id: string }) => {
			prepare('edu_experience', [{ _id: d._id }]);//del 教育经历
		});
	}
	const ret = await exec()
	if (ret.code === 1) {
		return 'success';
	} else {
		return 'fail';
	}
}
